function $ (x) {
    return document.getElementById(x)
}

window.onload = function () {
    if (isMobile()) {
        // 移动端时改变 CSS 样式表的引用状态和导航 logo 来源
        fixedStyle()
        $('csLogo').src = './img/logo1.svg'
        return
    }
    if (window.innerWidth < 750) $('csLogo').src = './img/logo1.svg'
    document.body.onscroll = function () { fixedScroll() }
    document.body.onresize = function () { fixedResize() }
}

// 监听高度变化
function fixedScroll () {
    // 不监听 PC 端的窄屏情况
    if (window.innerWidth < 750) return
    if (document.documentElement.scrollTop > 80) {
        $('nav').className = 'nav fixednav'
        $('hohead').style.marginTop = '240px'
        $('csLogo').src = './img/logo1.svg'
    } else {
        // 高度小于 80 时
        $('nav').className = 'nav'
        $('hohead').style.marginTop = '160px'
        // 宽度大于 750 时才换回白色 logo
        if (window.innerWidth > 750) {
            $('csLogo').src = './img/logo.svg'
        }
    }
}

// 监听宽度变化
function fixedResize () {
    if (window.innerWidth < 750) {
        $('csLogo').src = './img/logo1.svg'
    } else {
        $('csLogo').src = './img/logo.svg'
        // 清除从窄屏过渡时遗留的导航条问题
        $('aside').style.display = 'none'
        document.documentElement.style.overflow = 'auto'
    }
}

function isMobile() {
    var userAgentInfo = navigator.userAgent
    // 除了 "iPad" 外的其它所有主流移动平台
    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPod")
    var flag = false
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) { 
            flag = true 
            break 
        }
    }
    return flag;
}

function fixedStyle (cssLink, linkMedia) {
    $('pcNavLink').parentNode.removeChild($('pcNavLink'));
    $('pcMainLink').parentNode.removeChild($('pcMainLink'));
    $('mNavLink').setAttribute('media', 'all')
    $('mMainLink').setAttribute('media', 'all')
}

function showAside () {
    $('aside').style.display = 'block'
    $('header').style.display = 'none'
    // 禁止页面滚动
    document.documentElement.style.overflow = 'hidden'
}

function closeAside () {
    $('aside').style.display = 'none'
    $('header').style.display = 'block'
    // 允许页面滚动
    document.documentElement.style.overflow = 'auto'
}

// index.html 里的点击事件

function scroolToTarget () {
    var iID = setInterval(scroolToNext, 1)
    
    function scroolToNext () {
        var currentTop = window.pageYOffset
        var targetTop = window.innerHeight + 55
        if (currentTop > targetTop) clearInterval(iID)
        var nextTop = currentTop + 10
        window.scrollTo(0, nextTop)
    }
}

// follow.html 里的点击事件

function showfollowQr (sourceDivName) {
    // sourceDivName === 'cxyWechat' or 'csWechat'
    var targetDivName = sourceDivName + '_qr'
    $(sourceDivName).style.display = 'none'
    $(targetDivName).style.display = 'block'
}

function closefollowQr (sourceDivName) {
    // sourceDivName === 'cxyWechat_qr' or 'csWechat_qr'
    var targetDivName = sourceDivName.slice(0, -3)
    $(sourceDivName).style.display = 'none'
    $(targetDivName).style.display = 'block'
}